package server.topic.impl;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import javax.swing.tree.DefaultMutableTreeNode;

import org.omg.CORBA.Object;

import pds.uchiha.corba.manager.CorbaManager;


import server.accessers.impl.DataReaderImpl;
import server.datas.DataObject;
import server.domaines.impl.LocalDomaineManagerImpl;
import server.interfaces.Suscriber;
import server.interfaces.SuscriberHelper;
import server.interfaces.SuscriberPOATie;
import server.interfaces.impl.SuscriberImpl;
import server.topic.TopicOperations;

public class TopicImpl implements TopicOperations{
	private Hashtable<String, TopicImpl> topics;
	private DataObject data;
	private String name;
	private String fullName;
	private SuscriberImpl suscriber;
	private static int ic = 0;
	
	private LocalDomaineManagerImpl domaine;
	
	public void setDomaine(LocalDomaineManagerImpl domaine) {
		this.domaine = domaine;
	}
	
	public TopicImpl(String name, String parent) {
		ic++;
		this.name = name;
		if(parent.equals(""))
			this.fullName = name;
		else
			this.fullName = parent + "." + name;
		suscriber = new SuscriberImpl();
		suscriber.setDataReaderListener1(new DataReaderImpl(this));
		topics = new Hashtable<String, TopicImpl>();
	}
	
	public DefaultMutableTreeNode createNode(){
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
		Set<String> keys = topics.keySet();
		Iterator<String> strings = keys.iterator();
		while(strings.hasNext()){
			String x = strings.next();
			DefaultMutableTreeNode node1 = topics.get(x).createNode();
			node.add(node1);
		}
		return node;
	}

	@Override
	public DataObject getDataObject() {
		return data;
	}

	@Override
	public String getName() {
		return name;
	}
	
	public Suscriber getSuscriberWithPath(String topicName){
		int x = topicName.indexOf(".");
		if(x < 0){
			TopicImpl topic = topics.get(topicName);
			if(topic == null)
				return null;
			return topic.getSuscriber();
		}
		String topicN = topicName.substring(0, x);
		TopicImpl topic = topics.get(topicN);
		if(topic == null)
			return null;
		return topic.getSuscriberWithPath(topicName.substring(x + 1, topicName.length()));
	}

	@Override
	public Suscriber getSuscriber() {
		SuscriberPOATie suscriber = new SuscriberPOATie(this.suscriber);
		Object obj = CorbaManager.getInstance().publish(suscriber, "suscriber " + ic);
		return SuscriberHelper.narrow(obj);
	}

	@Override
	public boolean createTopic(String topicName) {
		TopicImpl topic = new TopicImpl(topicName, fullName);
		topic.setDomaine(domaine);
		boolean res = topics.put(topicName, topic) != null;
		if(domaine != null)
			domaine.showFrame();
		return res;
	}

	@Override
	public boolean deleteTopic(String topicName) {
		boolean res = topics.remove(topicName) != null;
		if(domaine != null)
			domaine.showFrame();
		return res;
	}

	@Override
	public boolean createCache() {
		data = new DataObject();
		return data != null;
	}

	@Override
	public boolean deleteCache() {
		data = null;
		return data == null;
	}
	@Override
	public String toString() {
		return name;
	}

	public String getFullName() {
		return fullName;
	}
}
